/*
 * @Date: 2024-01-12 17:19:44
 * @LastEditors: truman
 * @LastEditTime: 2024-02-25 20:01:05
 * @FilePath: \newbee-mall-vue3-app\src\utils\axios.js
 */
/**
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 */
 import axios from 'axios'
 import { showToast, showFailToast, showLoadingToast   } from 'vant'
 import { setLocal } from '@/common/js/utils'
 import router from '../router'

 let toast = null

 function startLoading() {
  // 开始加载
  toast = showLoadingToast({
      message: 'loading...',
      forbidClick: true,
  });
}
function endLoading() {
  // 结束加载
  toast.close()
}
 
 axios.defaults.baseURL = import.meta.env.MODE == 'development' ? '//47.94.236.45:5000' : '//jz.bcichsd.com:5000'
//  axios.defaults.baseURL = import.meta.env.MODE == 'development' ? '//47.94.236.45:5000' : '//47.94.236.45:5000'
 axios.defaults.withCredentials = true
 axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
 axios.defaults.headers['token'] = localStorage.getItem('token') || ''
 axios.defaults.headers['Authorization'] = localStorage.getItem('access_token')
 axios.defaults.headers.post['Content-Type'] = 'application/json'

 axios.interceptors.request.use(config => {
  startLoading()
  const hasToken = localStorage.getItem('access_token')
  if (hasToken) {
    config.headers['Authorization'] = hasToken
  }

  return config
 })
 
 axios.interceptors.response.use(res => {
  endLoading()
   if (typeof res.data !== 'object') {
    showFailToast('服务端异常！')
     return Promise.reject(res)
   }
  //  if (res.data.resultCode != 200) {
  //    if (res.data.message) showFailToast(res.data.message)
  //    if (res.data.resultCode == 416) {
  //      router.push({ path: '/login' })
  //    }
  //    if (res.data.data && window.location.hash == '#/login') {
  //      setLocal('token', res.data.data)
  //      axios.defaults.headers['token'] = res.data.data
  //    }
  //    console.log(99999, res.data);
  //    return Promise.reject(res.data)
  //  }
 
   return res.data
 })
 
 export default axios
 